home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / MOR55SRC.ZIP / MORIA / IBMPC / TCIO.C < prev    next >
C/C++ Source or Header  |  1992-12-08  |  9KB  |  432 lines

  1. /* ibmpc/tcio.c: terminal I/O code for Turbo C
  2.  
  3.    Copyright (c) 1989-92 James E. Wilson, Eric Vaitl
  4.  
  5.    This software may be copied and distributed for educational, research, and
  6.    not for profit purposes provided that this copyright and statement are
  7.    included in all such copies. */
  8.  
  9. /* This I/O module doesn't need PCcurses.  You may also need to make some
  10.    changes to ms_misc.c.  */
  11.  
  12. #include <alloc.h>  /* malloc() */
  13. #include <ctype.h>
  14. #include <dos.h>
  15. #include <stdlib.h>  /* getenv() */
  16. #include <stdio.h> /* putch */
  17. #include <process.h> /* spawnl() */
  18. #include <conio.h> /* window(), gotoxy() */
  19.  
  20. #ifdef __TURBOC__
  21. #include    <string.h>
  22. #endif /* __TURBOC__ */
  23.  
  24. #include "config.h"
  25. #include "constant.h"
  26. #include "types.h"
  27. #include "externs.h"
  28.  
  29. #define MSG_LEN  73
  30. #define LINES 25
  31. #define COLS 80
  32.  
  33. static void *savescr; /* pointer to a saved screen */
  34.  
  35. void init_curses(void){
  36.   if((savescr=malloc(LINES*COLS*2))==NULL){
  37.     puts("Out of memory in init_curses()");
  38.     exit(1);
  39.   }
  40.   clrscr();
  41.   msdos_raw();
  42. }
  43.  
  44.  
  45. int get_string(char *in_str,int row,int column,int slen)
  46. {
  47.   register int start_col, end_col, i;
  48.   char *p;
  49.   int flag, aborted;
  50.  
  51.   aborted = FALSE;
  52.   flag  = FALSE;
  53.   gotoxy(column+1,row+1);
  54.   for (i = slen; i > 0; i--)
  55.     putch(' ');
  56.   gotoxy(column+1,row+1);
  57.   start_col = column;
  58.   end_col = column + slen - 1;
  59.   if (end_col > 79)
  60.     {
  61.       slen = 80 - column;
  62.       end_col = 79;
  63.   }
  64.   p = in_str;
  65.   do
  66.     {
  67.       i = inkey();
  68.       switch(i)
  69.         {
  70.           case ESCAPE:
  71.           aborted = TRUE;
  72.           break;
  73.         case CTRL('J'): case CTRL('M'):
  74.           flag  = TRUE;
  75.           break;
  76.         case DELETE: case CTRL('H'):
  77.           if (column > start_col)
  78.             {
  79.               column--;
  80.               put_buffer(" ", row, column);
  81.               move_cursor(row, column);
  82.               *--p = '\0';
  83.           }
  84.           break;
  85.         default:
  86.           if (!isprint(i) || column > end_col)
  87.             bell();
  88.           else
  89.             {
  90.               gotoxy(column+1,row+1);
  91.               putch((char) i);
  92.               *p++ = i;
  93.               column++;
  94.           }
  95.           break;
  96.       }
  97.   }
  98.   while ((!flag) && (!aborted));
  99.   if (aborted)
  100.     return(FALSE);
  101.   /* Remove trailing blanks     */
  102.   while (p > in_str && p[-1] == ' ')
  103.     p--;
  104.   *p = '\0';
  105.   return(TRUE);
  106. }
  107.  
  108. void put_buffer(char *out_str,int row,int col){
  109.   vtype tmp_str;
  110.   if (col>79) col=79;
  111.   strncpy(tmp_str,out_str,79-col);
  112.   tmp_str[79-col]='\0';
  113.   gotoxy(col+1,row+1);
  114.   cputs(tmp_str);
  115. }
  116.  
  117. void put_qio(void){
  118. /* nothing to do */
  119. }
  120.  
  121. void restore_term(void){
  122.   fflush(stdout);
  123.   clear_screen();
  124.   msdos_noraw();
  125. }
  126.  
  127. void shell_out(void){
  128.   char *comspec;
  129. #ifndef __TURBOC__
  130.   char key;
  131.   int val;
  132.   char *str;
  133. #endif /* __TURBOC__ */
  134.   save_screen();
  135.   clear_screen();
  136.   puts("[Entering DOS shell, type exit to return to game.]");
  137.   msdos_noraw();
  138.   ignore_signals();
  139.   if((comspec=getenv("COMSPEC")) ==NULL ||
  140.     spawnl(P_WAIT,comspec,comspec,(char *)NULL)<0){
  141.     puts("Sorry, there seems to be a problem with shell_out()");
  142.     printf("comspec = %s\n",comspec);
  143.     flush();
  144.     puts("Hit a key to continue");
  145.     while(!kbhit())
  146.       ;
  147.   }
  148.   restore_signals();
  149.   restore_screen();
  150. }
  151.  
  152. void save_screen(void){
  153.   gettext(1,1,COLS,LINES,savescr);
  154. }
  155.  
  156. void restore_screen(void){
  157.   puttext(1,1,COLS,LINES,savescr);
  158. }
  159.  
  160. void clear_screen(void){
  161.   window(1,1,COLS,LINES);
  162. /*
  163. I think later I might want to define seperate windows, so the above line
  164. is definsive code.
  165. */
  166.   clrscr();
  167. }
  168.  
  169. void clear_from(int row){
  170.   window(1,row+1,COLS,LINES);
  171.   clrscr();
  172.   window(1,1,COLS,LINES);
  173. }
  174.  
  175. void flush(void){
  176.   while(kbhit())
  177.     getch();
  178. }
  179.  
  180. void erase_line(int row, int col){
  181.   if(row==MSG_LINE&&msg_flag)
  182.     msg_print(NULL);
  183.   gotoxy(col+1,row+1);
  184.   clreol();
  185. }
  186.  
  187. char inkey(void){
  188.   int i;
  189.  
  190.   command_count=0;
  191.   while(TRUE){
  192.     i=msdos_getch();
  193.     if(i==EOF){
  194.       eof_flag++;
  195.       msg_flag=FALSE;
  196.       if(!character_generated||character_saved) exit_game();
  197.       disturb(1,0);
  198.       if(eof_flag>100){
  199.         panic_save=1;
  200.         strcpy(died_from,"(end of input: panic saved)");
  201.         if(!save_char()){
  202.           strcpy(died_from,"panic: unexpected eof");
  203.           death=TRUE;
  204.         }
  205.         exit_game();
  206.       }
  207.     return ESCAPE;
  208.   }
  209.   if(i!=CTRL('R'))
  210.     return (char) i;
  211.   msdos_raw();
  212.     break;
  213.   }
  214.   return (CTRL('R'));
  215. }
  216.  
  217. void print(char ch, int row, int col){
  218.   row -= panel_row_prt;
  219.   col-=panel_col_prt;
  220.   gotoxy(col+1,row+1);
  221.   putchar((int)ch);
  222. }
  223.  
  224. void move_cursor_relative(int row, int col){
  225.   row-=panel_row_prt;
  226.   col-=panel_col_prt;
  227.   gotoxy(col+1,row+1);
  228. }
  229.  
  230. void count_msg_print(char *p){
  231.   int i;
  232.   i=command_count;
  233.   msg_print(p);
  234.   command_count=i;
  235. }
  236.  
  237. void prt(char* str_buff,int row, int col){
  238.   if (row==MSG_LINE&&msg_flag)
  239.     msg_print(NULL);
  240.   gotoxy(col+1,row+1);
  241.   clreol();
  242.   put_buffer(str_buff,row,col);
  243. }
  244.  
  245. void move_cursor(int row,int col){
  246.   gotoxy(col+1,row+1);
  247. }
  248.  
  249. void msg_print(char* str_buff){
  250.   register int old_len;
  251.   char in_char;
  252.   if(msg_flag){
  253.     old_len=strlen(old_msg[last_msg])+1;
  254.     if (old_len>MSG_LEN)
  255.       old_len=MSG_LEN;
  256.     put_buffer("-more-",MSG_LINE, old_len);
  257.     wait_for_more=1;
  258.     do{
  259.       in_char=inkey();
  260.     }while((in_char!=' ')&&(in_char!=ESCAPE)&&(in_char!='\n')&&
  261.        (in_char!='\r'));
  262.     wait_for_more=0;
  263.   }
  264.   gotoxy(1,MSG_LINE+1);
  265.   clreol();
  266.   if(str_buff){
  267.     put_buffer(str_buff,MSG_LINE,0);
  268.     command_count=0;
  269.     if(++last_msg>=MAX_SAVE_MSG) last_msg=0;
  270.     strncpy(old_msg[last_msg],str_buff,VTYPESIZ);
  271.     old_msg[last_msg][VTYPESIZ-1]='\0';
  272.     msg_flag=TRUE;
  273.   }else
  274.     msg_flag=FALSE;
  275. }
  276.  
  277. int get_check(char*prompt){
  278.   int res;
  279.   prt(prompt,0,0);
  280.   if(wherex()>MSG_LEN +1) gotoxy(74,1);
  281.   cputs(" [y/n]");
  282.   do{
  283.     res=inkey();
  284.   }while(res==' ');
  285.   erase_line(0,0);
  286.   if(res=='Y'||res=='y')
  287.     return(TRUE);
  288.   else
  289.     return(FALSE);
  290. }
  291.  
  292. int get_com(char *prompt,char *command){
  293.   int res;
  294.   if(prompt)
  295.     prt(prompt,0,0);
  296.   *command=inkey();
  297.   if(*command==ESCAPE)
  298.     res=FALSE;
  299.   else
  300.     res=TRUE;
  301.   erase_line(MSG_LINE,0);
  302.   return(res);
  303. }
  304. void bell(void){
  305.   if (! sound_beep_flag)
  306.     return;
  307.  
  308.   putchar('\007');
  309. }
  310.  
  311. /* the rest is just modified -ev- */
  312.  
  313. /* definitions used by screen_map() */
  314. /* index into border character array */
  315. #define TL 0    /* top left */
  316. #define TR 1
  317. #define BL 2
  318. #define BR 3
  319. #define HE 4    /* horizontal edge */
  320. #define VE 5
  321.  
  322. /* character set to use */
  323. #   define CH(x)        (screen_border[1][x])
  324.  
  325.   /* Display highest priority object in the RATIO by RATIO area */
  326. #define RATIO 3
  327.  
  328. void screen_map()
  329. {
  330.     register int  i, j;
  331.   static int8u screen_border[2][6] = {
  332.       {'+', '+', '+', '+', '-', '|'},     /* normal chars */
  333.     {201, 187, 200, 188, 205, 186}      /* graphics chars */
  334. };
  335.   int8u map[MAX_WIDTH / RATIO + 1];
  336.   int8u tmp;
  337.   int priority[256];
  338.   int row, orow, col, myrow, mycol = 0;
  339.   char prntscrnbuf[80];
  340.  
  341.   for (i = 0; i < 256; i++)
  342.     priority[i] = 0;
  343.   priority['<'] = 5;
  344.   priority['>'] = 5;
  345.   priority['@'] = 10;
  346.   priority[wallsym] = -5;
  347.   priority[floorsym] = -10;
  348.   priority['\''] = -3;
  349.   priority[' '] = -15;
  350.  
  351.   save_screen();
  352.   clear_screen();
  353.   gotoxy(1,1);
  354.   putch(CH(TL));
  355.   for (i = 0; i < MAX_WIDTH / RATIO; i++)
  356.     putch(CH(HE));
  357.   putch(CH(TR));
  358.   orow = -1;
  359.   map[MAX_WIDTH / RATIO] = '\0';
  360.   for (i = 0; i < MAX_HEIGHT; i++)
  361.     {
  362.         row = i / RATIO;
  363.       if (row != orow)
  364.         {
  365.             if (orow >= 0)
  366.             {
  367.                sprintf(prntscrnbuf,"%c%s%c",CH(VE), map, CH(VE));
  368.                gotoxy(1,orow+2);
  369.                cputs(prntscrnbuf);
  370.           }
  371.           for (j = 0; j < MAX_WIDTH / RATIO; j++)
  372.             map[j] = ' ';
  373.           orow = row;
  374.       }
  375.       for (j = 0; j < MAX_WIDTH; j++)
  376.         {
  377.             col = j / RATIO;
  378.           tmp = loc_symbol(i, j);
  379.           if (priority[map[col]] < priority[tmp])
  380.             map[col] = tmp;
  381.           if (map[col] == '@')
  382.             {
  383.                 mycol = col + 1; /* account for border */
  384.               myrow = row + 1;
  385.           }
  386.       }
  387.   }
  388.   if (orow >= 0)
  389.     {
  390.       sprintf(prntscrnbuf,"%c%s%c",CH(VE), map, CH(VE));
  391.       gotoxy(1,orow+2);
  392.       cputs(prntscrnbuf);
  393.   }
  394.   gotoxy(1,orow+3);
  395.   putch(CH(BL));
  396.   for (i = 0; i < MAX_WIDTH / RATIO; i++)
  397.     putch(CH(HE));
  398.   putch(CH(BR));
  399.   gotoxy(24,24);
  400.   cputs("Hit any key to continue");
  401.   if (mycol > 0)
  402.     gotoxy(mycol+1, myrow+1);
  403.   inkey();
  404.   restore_screen();
  405. }
  406.  
  407. void pause_exit(int prt_line, int delay)
  408. {
  409.     char dummy;
  410.  
  411. #ifdef __TURBOC__
  412.   /* Otherwise, TURBO C complains that delay is never used.  */
  413.   dummy = (char) delay;
  414. #endif
  415.   prt("[Press any key to continue, or Q to exit.]", prt_line, 10);
  416.   dummy = inkey();
  417.   if (dummy == 'Q')
  418.     {
  419.       erase_line(prt_line, 0);
  420.       exit_game();
  421.   }
  422.   erase_line(prt_line, 0);
  423. }
  424.  
  425. void pause_line(int prt_line)
  426. {
  427.   prt("[Press any key to continue.]", prt_line, 23);
  428.   (void) inkey();
  429.   erase_line(prt_line, 0);
  430. }
  431.  
  432.